﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Com.Gcssloop.Widget;
using FFImageLoading;
using FFImageLoading.Views;
using mb;
using mb.Droid;
using static Android.Support.V7.Widget.GridLayoutManager;

namespace libmp
{
    class AdImageView
    {
        protected static ILog logger = App.logger;
        RCRelativeLayout rootLayout;
        ViewPager pager;
        int startIndex;
        int endIndex;

        protected StartActivity activity { get => StartActivity.This; }

        public AdImageView(int s, int e)
        {
            startIndex = s;
            endIndex = e;
            DataStore.store.OnChanged += (store) =>
            {
                Update(store);
            };
        }

        void Update(DataStore store)
        {
            logger.Log("AdImageView update", store.onlineConfig.AdImages.Count);
            if( store.onlineConfig == null ) return;
            var views = new List<View>();
            var data = store.onlineConfig.AdImages.Skip(startIndex).Take(endIndex - startIndex + 1);
            foreach( var img in data ) {
                var view = new ImageView(activity);
                view.SetScaleType(ImageView.ScaleType.FitXy);
                view.Click += (s, e) =>
                {
                    Xamarin.Essentials.Browser.OpenAsync(img.Target, Xamarin.Essentials.BrowserLaunchMode.External);
                    // User.me.Notify("clickad", LitJson.JsonMapper.ToJson(new { imgid = img.Id }));
                };
                views.Add(view);
                FFImageLoading.ImageService.Instance.LoadUrl(img.Url).Into(view);
            }

            pager.Adapter = new MySimplePagerAdapter() { Views = views };
        }

        public virtual View CreateView()
        {
            rootLayout = new RCRelativeLayout(activity);
            rootLayout.SetRadius(20);
            rootLayout.ClipBackground = true;

            rootLayout.AddView(
                pager = new ViewPager(activity) { Adapter = new MySimplePagerAdapter() { Views = new List<View>() } },
                new RelativeLayout.LayoutParams(-1, -1) { }
            );

            var pid = new ViewPagerIndicator.CirclePageIndicator(activity);
            pid.SetStrokeWidth(0);
            pid.SetPageColor(Color.Rgb(223, 234, 253));
            pid.SetFillColor(Color.Rgb(251, 114, 153));
            pid.SetRadius(15);
            pid.SetViewPager(pager);

            rootLayout.AddView(
                pid,
                new RelativeLayout.LayoutParams(-1, 50)
                .AddRuleEx(LayoutRules.AlignParentBottom)
                .AddRuleEx(LayoutRules.CenterHorizontal)
            );

            Update(DataStore.store);

            return rootLayout;
        }
    }
}